define(["require",
	'config/appregister',
	'business/blogpost/service/blogHttp',
	"jquery",
	"domReady",
	"business/home/directives/editorDirective",
	"business/home/services/filterService"], function(require, app, httpServices) {
	app.controller('blogmodCtrl', function($scope, $http, $state, $stateParams) {
		$scope.checkMajor='';
		$scope.checkCon='';
		$scope.checkEng='';
		$scope.data = {
			"title": "",
			"content": "",
			"labelIds": "",
			"tcId": "",
			"scId": "",
			"dcId": "",
			"price": "",
			"remindType": ""
		};
		//取博文id和type
		if(!angular.isUndefined($stateParams.blogid,$stateParams.blogType)) {
			$scope.modid = $stateParams.blogid;
			$scope.modtype = $stateParams.blogType;
		}
		
		var modid = $scope.modid;
		var modtype = $scope.modtype;
		
		//博文详情回显
		getinfo();
		
		
		//确认修改动作	
		$scope.modify = function(){
		  if($scope.tags2) {
		  	var tags = $scope.tags2.join(",");
			getlabelIds(tags);
		  }
		};
		//博文详情回显
		$scope.controlLength = function(str){
			var l = str.length;
			var blen = 0;
			for(var i=0; i<l; i++) {
				if (str.charCodeAt(i) > 255) {
					blen ++;
				}
				blen ++;
			}
			if(blen == 32){
				var maxLength = $scope.tagsWork.length;
			}
			if(blen > 32){
				$scope.tagsWork.length = maxLength;
			}
		};
		$scope.tags2 = [//ngInputTab自己写方法

        ];
        $scope.saveTagsname=function(a){
			if(a == ''){
				return;
			}
			if($scope.tags2.length == 6){
				alert('最多只能添加六个标签哦，亲');
				return;
			}
			if($scope.tags2.indexOf(a) != -1){
				alert("关键词不能相同哦")
			}else{
				$scope.tags2.push(a);
				$scope.tagsWork = '';
			}
        };
        $scope.deleteInput=function($index){
            $scope.tags2.splice($index,1);
        };
		$scope.isSelected=function(id){
			if(id==$scope.checkMajor){
				return true;
			}else{
				return false;
			}
		};
        //获取博文详情
		function getinfo() {
			var req = {
				type:'GET',
				uri:'getInfo',
				data:{
					blogId:modid
				}
			};
			httpServices.getblogHttpService(req).success(function(data){
				if(!data)return;
				if(data.code==0){
					console.log(data.data);
					var echo = data.data.blog;
					var old = data.data.blog.dcList;
					$scope.$apply(function(){
						//分类回显
						$scope.dcBlog = data.data.blog.dcList;
						if(data.data.blog.tcId === 2) {
							$scope.getsecond(2, 1);
							$(".iask-please .fuckul li").eq(1).css('color','#30a3ff').siblings().css('color','#666');
							var theindex;
							if(data.data.blog.scId == 15) {
								theindex = 0;
							}
							if(data.data.blog.scId == 16) {
								theindex = 1;
							}
							if(data.data.blog.scId == 17) {
								theindex = 2;
							}
							for(var i=0;i<data.data.blog.dcList.length;i++){
								if(data.data.blog.dcList[i].detailCategory.dcType==1){
									$scope.checkMajor=data.data.blog.dcList[i].dcId
								}
								if(data.data.blog.dcList[i].detailCategory.dcType==2){
									$scope.checkCon=data.data.blog.dcList[i].dcId
								}
								if(data.data.blog.dcList[i].detailCategory.dcType==3){
									$scope.checkEng=data.data.blog.dcList[i].dcId;
									console.log($scope.checkEng);
								}
							}
							//if(data.data.blog.dcList[0]){
							//	$scope.checkMajor = data.data.blog.dcList[0].dcId;
							//}
							//if(data.data.blog.dcList[1]){
							//	$scope.checkCon = data.data.blog.dcList[1].dcId
							//}
							//if(data.data.blog.dcList[2]){
							//	$scope.checkEng = data.data.blog.dcList[2].dcId
							//}
							//if(data.data.blog.dcList[3]){
							//	$scope.check3 = data.data.blog.dcList[3].dcId
							//}
							$scope.getscid(data.data.blog.scId,theindex);
							var theradio1 = $("input[name='radio1']");
							var thedclist = data.data.blog.dcList;
							
							$scope.huixian1 = function(a) {
								if(a==173){
									return true;
								};
							};
						}else if(data.data.blog.tcId === 1){
							$scope.getsecond(1, 0);
							$(".iask-please .fuckul li").eq(0).css('color','#30a3ff').siblings().css('color','#666');
							$scope.data.tcid = data.data.blog.tcId;
							$scope.getthree(data.data.blog.scId);
							$scope.data.scid = data.data.blog.scId;
							$scope.getthis(data.data.blog.dcList[0].dcId);
							$scope.data.dcId = data.data.blog.dcList[0].dcId;
						}
						
						var oldarr = [];
						var oldname = [];
						for(var z = 0;z < old.length;z++) {
							oldname.push(old[z].detailCategory.dcName);
						}
						for(var i = 0;i < old.length;i++) {
							oldarr.push(old[i].dcId); 
						}
						if(!oldarr) {
							$scope.oldDcId = 0;
						}
						
						for(var i = 0;i < echo.labelList.length;i++) {
							$scope.tags2.push(echo.labelList[i].labelName);
						}
				      	$scope.title = echo.title;
				      	$scope.tips  = echo.labelIds;
				      	$scope.oneid = echo.topCategory.tcName;
				      	$scope.twoid = echo.secondaryCategory.scName;
						$scope.content = echo.introduction;
						$scope.oldDcId = oldarr.join(",");
						$scope.thetcid = echo.tcId;
						$scope.thescid = echo.scId;
						$scope.thedcids = oldarr.join(",");
				    });
				}else {
					alert(data.msg);
				}
			}).error(function(data){
				console.log("修改博文详情回显请求失败");
			});			
		};
		
		//选择分类
		getonelist();
		function getonelist(){
			var req = {
				type:'GET',
				uri:'getTopCategory',
			};
			httpServices.wtfHttpService(req).success(function(data){
				if(!data)return;
				$scope.$apply(function(){
					if(data.code==0) {
						$scope.onelist = data.data;
						console.log($scope.onelist);
					}else {
						alert(data.msg);
					}
				})

			}).error(function(data){
				alert("选择分类请求失败");
			});	
		};
		
		//34级分类
		$scope.getscid = function(scid, $index) {
			if($index == 0) {
				$(".h21").html("国家：");
				$(".h22").html("专业：");
				$(".h23").html("种类：");
				$(".h24").hide();
				$(".yiwai").hide();
			}else if($index == 1){
				$(".h21").html("专业：");
				$(".h22").html("内容：");
				$(".h23").html("项目模式：");	
				$(".h24").hide();
				$(".yiwai").hide();
			}else if($index == 2){
				$(".h24").show();
				$(".yiwai").show();
				$(".h21").html("经济环境：");
				$(".h22").html("法律法规：");
				$(".h23").html("合同管理：");	
				$(".h24").html("宗教文化：");	
			}
			
			var req = {
				type:'GET',
				uri:'getDetailCategory',
				data:{
					scId:scid
				}
			};
			httpServices.wtfHttpService(req).success(function(data){
				if(!data)return;
				if(data.code==0) {
					$scope.$apply(function(){
						$scope.data.scid = scid;
						var thebox = $(".dandan");
						var max = thebox[$index];
						var ss = $(max).next();
						var ele = $(".showbox");
						ele.hide();
						ss.show();
						var zhuanye = [];
						var leirong = [];
						var moshi = [];
						var zongj = [];
						var four = data.data;
						for(i in four) {
							if(four[i].dcType == 1 || four[i].dcType == 4 || four[i].dcType == 7) {
								zhuanye.push(four[i]);
							} else if(four[i].dcType == 2 || four[i].dcType == 5 || four[i].dcType == 8) {				
								leirong.push(four[i]);
							} else if(four[i].dcType == 3 || four[i].dcType == 6 || four[i].dcType == 9) {
								moshi.push(four[i]);
							} else if(four[i].dcType == 10) {
								zongj.push(four[i]);
							}
						}
						$scope.zy = zhuanye;
						$scope.lr = leirong;
						$scope.ms = moshi;
						$scope.duo = zongj;
					});
				}else {
					alert(data.msg);
				}
			}).error(function(){
				alert("34级分类请求失败");
			});
		}

		//
		$scope.getthis = function($index,dcid) {
				$(".iask-three ul li").eq($index).css('color','#30a3ff').siblings().css('color','#666');
				$scope.data.dcId = dcid;
		}
		//

		$scope.getthree = function(scid, $index) {
				$(".iask-pleaseed ul li").eq($index).css('color','#30a3ff').siblings().css('color','#666');
				$(".iask-three").show();
				var req = {
					type:'GET',
					uri:'getDetailCategory',
					data:{
						scId:scid
					}
				};
				httpServices.wtfHttpService(req).success(function(data){
					if(!data)return;
					$scope.$apply(function(){
						if(data.code==0) {
							$scope.three = data.data;
							$scope.data.scid = scid;
						}else {
							alert(data.msg);
						}						
					})
				}).error(function(data){
					alert('渲染三级分类请求失败');
				})
		};
		
		
		//获取二级分类列表
		$scope.getsecond = function(tcid, $index) {
			$(".iask-please .fuckul li").eq($index).css('color','#30a3ff').siblings().css('color','#666');
			if(tcid == 2) {
				$scope.data.dcId = undefined;
				$(".iask-pleaseed").hide();
				$(".iask-three").hide();
				$(".iask-them").show();
				var req = {
					type:'GET',
					uri:'getSecondaryCategory',
					data:{
						tcId:tcid
					}
				};
				httpServices.wtfHttpService(req).success(function(data){
					if(!data)return;
					$scope.$apply(function(){
						if(data.code==0) {
							$scope.them = data.data;						
						}else {
							alert(data.msg);
						}						
					})
				}).error(function(){
					alert("渲染国际工程里的list失败");
				});

			} else {
				$scope.radiosarr = [];
				$(".iask-them").hide();
				$(".iask-three").hide();
				$(".iask-pleaseed").show();
				var req = {
					type:'GET',
					uri:'getSecondaryCategory',
					data:{
						tcId:tcid
					}
				};
				httpServices.wtfHttpService(req).success(function(data){
					if(!data)return;
					$scope.$apply(function(){
						if(data.code==0) {
							$scope.twolist = data.data;						
						}else {
							alert(data.msg);
						}						
					})

				}).error(function(){
					alert("渲染二级分类失败");
				});
			}
			$scope.data.tcId = tcid;
		};

		//拿标签id
		function getlabelIds(a) {
			var req = {
				type:'POST',
				uri:'addBatch',
				data:{
					tags: a
				}
			};
			httpServices.gettabHttpService(req).success(function(data){
				if(!data)return;
				$scope.$apply(function() {
					$scope.labelids = data.data;
					if(!!$scope.data.scid) {
					 	 //提交修改
				 		 modBlogPost();						
					}else {
						$scope.data.scid = $scope.thescid;
						$scope.data.tcId = $scope.thetcid;
						$scope.oldDcId = $scope.thedcids;
						modBlogPost();
					}
				});
			}).error(function() {
				console.log("标签请求失败");
			});
			
		};
		//确认修改提交请求
		function modBlogPost() {	
			var commentopen = $('.one input[name="rad"]:checked').val();
			var open = $('.one input[name="rad1"]:checked').val();
			var radio1 = $scope.checkMajor;
			var radio2 = $scope.checkCon;
			var radio3 = $scope.checkEng;
			$scope.radiosarr = [];
			if(radio1) {
				$scope.radiosarr.push(radio1);
			}
			if(radio2) {
				$scope.radiosarr.push(radio2);
			}
			if(radio3) {
				$scope.radiosarr.push(radio3);
			}
			if($scope.radiosarr.length == 0) {
				if($scope.data.dcId) {
					var dcids = $scope.data.dcId;
				}else {
					alert("请选择详细分类");
					return false;
				}
			}else {
				var dcids = $scope.radiosarr.join(",");
			}
			
			if(!modtype) {
				modtype = 0;
			}
			var req = {
				type:'POST',
				uri:'update',
				data:{
					blogId: modid,
					title: $scope.title,
					labelIds: $scope.labelids,
					tcId: $scope.data.tcId,
					scId: $scope.data.scid,
					dcIds: dcids,
					oldDcIds: $scope.oldDcId,
					open: open,
					commentopen: commentopen,
					type: modtype,
					introduction: $scope.content				
				}
			};
			$.ajax({
				type: "post",
				dataType: "json",
				data:req.data,
				url: "http://"+allUrl+":8080/elink_user/api/blog/update",
				success: function(data) {
					if(!data)return;
					$scope.$apply(function(){
						if(data.code==0) {
							alert("修改博文成功！");
							$("#qued").unbind("click");
							if($stateParams.blogType==1){
								$state.go("comIndexCtrl.resource");
							}else{
								$state.go("bloglist");
							}
						}else {
							alert(data.msg);
						}						
					});
				},
				error: function(jqXHR) {
					alert("提交博文请求失败");
				}
			});			
		};

	});
});